
import os
import sys
import shlex
from pathlib import Path

BASE_DIR = Path(__file__).resolve().parent.parent
sys.path.append(os.path.dirname(os.path.dirname(os.path.realpath(__file__))))

from utils.pyssh import  SSHProxy
from utils.custom_argparse import parse_args
from utils.custom_log import log_start
logger = log_start('main')


def exec_cmd(adminkey,remote_user,password,port,command,show):
    
    hostsfile = os.path.join(BASE_DIR, 'config/hosts.txt')
    with open(hostsfile) as f:
        for line in f:
            line = line.strip().split()
            if password:
                passwd = password
            else:
                passwd =line[1]
            host = line[0]

            try:
                with SSHProxy(host, port, remote_user, passwd) as ssh:
                    ssh.command(command,show)
            except Exception as e:
                print(e)
                pass


if __name__ == '__main__':
    # logger.error("test")
    if len(sys.argv) == 1:
        """ 如果没有传参则打印帮助信息 """
        sys.argv.append("--help")
        parse_args()
        sys.exit(0)

    params = parse_args()  # 获取传入的参数
    exec_cmd(params.sshkey,params.username,params.password,params.port,params.cmd,show=params.show)
